//
//  SISSWroteCommentViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/5/3.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSWroteCommentViewController.h"
#import "SISSCommentCell.h"
#import "SISSOrderEvaluateViewCell.h"
#import "SISSNetworkingManager+Comment.h"


NSString *commentCustomerCellReuseID =  @"commentCustomerCellReuseID";
NSString *commentBrokerCellReuseID = @"commentBrokerCellReuseID";
@interface SISSWroteCommentViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet SISSBaseTableView *tableView;

/* 评论数组 */
@property (strong, nonatomic) NSMutableArray<SISSCommentModel *> *commentArr;

/** pageNo */
@property (assign, nonatomic) NSInteger pageNo;

/// commentType 1带看 2服务
@property (assign, nonatomic) NSInteger commentType;

@end

@implementation SISSWroteCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.pageNo = 1;
    self.commentType = 1;
    [self requestAgentSendCommentWithCommentType:1];
    
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
    
    
    [kSISSRefresh prepareFooterRefresh:self.tableView reloadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeReloadMore;
        [weakSelf reloadMoreData];
    }];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWithMessage:@"没有相关数据！" ifNecessaryForRowCount:self.commentArr.count];
    return self.commentArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.commentArr[indexPath.row].comment_per_type == 3) {
        SISSOrderEvaluateViewCell *cell = [tableView dequeueReusableCellWithIdentifier:commentBrokerCellReuseID forIndexPath:indexPath];
        cell.commentModel = self.commentArr[indexPath.row];
        return cell;
    } else {
         SISSCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:commentCustomerCellReuseID forIndexPath:indexPath];
        cell.commentModel = self.commentArr[indexPath.row];
        
        return cell;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - 上下拉刷新加载方法
- (void)upDownLoadData {
    self.pageNo = 1;
    if (self.commentType == 1) {
        [self requestAgentSendCommentWithCommentType:1];
    } else if (self.commentType == 2) {
        [self requestAgentSendCommentWithCommentType:2];
    }
}

- (void)reloadMoreData {
    
    self.pageNo += 1;
    if (self.commentType == 1) {
        [self requestAgentSendCommentWithCommentType:1];
    } else if (self.commentType == 2) {
        [self requestAgentSendCommentWithCommentType:2];
    }
}

#pragma mark - 请求数据

/**
 数据的请求

 @param commentType 1:带看评价  2:服务评价
 */
- (void)requestAgentSendCommentWithCommentType:(NSInteger)commentType {
    
    WeakSelf
    [kNetworkManager requestAgentSendCommentInfoWithCommentType:commentType pageNo:self.pageNo success:^(SISSCommentBaseModel *commentBaseModel) {
        
        if (weakSelf.commentArr.count != 0 && kSISSRefresh.refreshType == SISSRefressTypeUpDown) {
            
            [weakSelf.commentArr removeAllObjects];
        }
        
        for (int i = 0; i < commentBaseModel.data.count; i++) {
            
            [weakSelf.commentArr addObject:commentBaseModel.data[i]];
        }

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
            
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];
            if (weakSelf.commentArr.count >= commentBaseModel.total) {
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            if (weakSelf.commentArr.count == 0) {
                self.tableView.mj_footer.hidden = YES;
            }
        }];
        
    } failed:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        NSLog(@"%@",error);
    }];
}



#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    // tableView相关
    // 自动行高
    self.tableView.estimatedRowHeight = 120;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }
    // 去掉cell系统的线条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSCommentCell" bundle:nil] forCellReuseIdentifier:commentCustomerCellReuseID];
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSOrderEvaluateViewCell" bundle:nil] forCellReuseIdentifier:commentBrokerCellReuseID];
}



#pragma mark - 点击segmentControl的响应事件
- (IBAction)segmentControlClickAction:(UISegmentedControl *)sender {
    self.pageNo = 1;
    if (sender.selectedSegmentIndex == 0) {
        [self requestAgentSendCommentWithCommentType:1];
        self.commentType = 1;
    } else {
        self.commentType = 2;
        [self requestAgentSendCommentWithCommentType:2];
    }
    [self.tableView reloadData];
    NSLog(@"selectedSegmentIndex:  %zd",sender.selectedSegmentIndex);
}

- (NSMutableArray<SISSCommentModel *> *)commentArr {
    if (!_commentArr) {
        _commentArr = [NSMutableArray array];
    }
    return _commentArr;
}

@end
